package com.seal.commons.utils.csv;

import org.apache.commons.csv.CSVFormat;
import org.apache.commons.csv.CSVPrinter;

import java.io.File;
import java.io.FileWriter;
import java.util.List;


/**
 * csv文件操作工具类
 */
public class CSVUtils {

	//CSV文件分隔符
	private final static String NEW_LINE_SEPARATOR="\n";
	//初始化csvformat
	private final static CSVFormat formator = CSVFormat.DEFAULT.withRecordSeparator(NEW_LINE_SEPARATOR);


	private static File createCsv(String[] headers, List<String[]> content) throws Exception {
		File file = File.createTempFile("temp", ".csv");
		createCsv(file, headers, content);
		return file;
	}


	public static void createCsv(File file,String[] headers, List<String[]> content) throws Exception {
		FileWriter fileWriter = new FileWriter(file);

		try(CSVPrinter printer = new CSVPrinter(fileWriter, formator)){
			printer.printRecord(headers);
			if(content != null){
				for(String[] data : content){
					printer.printRecord(data);
				}
			}
		}

	}

}
